﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using ComputerVision.Data;
using ComputerVision.Data.Repositories;

namespace ComputerVision.Services
{
    public class DoiService : IService<Doi>
    {
        private readonly IEntityRepository<Doi> _doigRepository;
        public DoiService(IEntityRepository<Doi> doigRepository)
        {
            _doigRepository = doigRepository;
        }

        public Doi Create(Doi t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _doigRepository.InsertOnCommit(t);
                _doigRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage) + Environment.NewLine;

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return t;
        }

        public Doi Update(Doi t)
        {
            Doi item;
            try
            {
                item = _doigRepository.Get(t.Id);
                item.TenDoi = t.TenDoi;
                item.DienGiai = t.DienGiai;
                item.Phong_Id = t.Phong_Id;
                //item.Created = DateTime.Now;
                //item.CreatedBy_Id = t.CreatedBy_Id;
                _doigRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine +
                               string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                                   validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
            return item;
        }

        public Doi Detail(Guid id)
        {
            var item = _doigRepository.Get(id);
            return item;
        }

        public List<Doi> ToList()
        {
            var item = _doigRepository.GetAll().ToList().OrderBy(d => d.TenDoi).ToList();
            return item;
        }

        public void Delete(Doi t)
        {
            try
            {
                if (t == null)
                    throw new ArgumentNullException("entity");
                _doigRepository.DeleteOnCommit(t);
                _doigRepository.CommitChanges();
            }
            catch (DbEntityValidationException dbEx)
            {
                var msg = string.Empty;

                foreach (var validationErrors in dbEx.EntityValidationErrors)
                    foreach (var validationError in validationErrors.ValidationErrors)
                        msg += Environment.NewLine + string.Format("Property: {0} Error: {1}", validationError.PropertyName, validationError.ErrorMessage);

                var fail = new Exception(msg, dbEx);
                throw fail;
            }
        }
    }
}
